<?php
session_start();
ini_set ("max_execution_time","3600");

define( 'DS', DIRECTORY_SEPARATOR );
define( 'WS', '/' );
define( 'ROOTPATH', dirname(__FILE__) );
//$mcookies = dirname(__FILE__).DIRECTORY_SEPARATOR.'cookie.txt';
$mcookies = "cookie.txt";
define( 'COOKIES', $mcookies );


function curl_postaction($url,$data,$proxy= '',$proxystatus = 'off'){
    $fp = fopen(COOKIES, "w");
    fclose($fp);
    $login = curl_init();
	
	curl_setopt($login, CURLOPT_COOKIEJAR, COOKIES);
	curl_setopt($login, CURLOPT_COOKIEFILE, COOKIES);
	curl_setopt($login, CURLOPT_FRESH_CONNECT, TRUE);
    curl_setopt($login, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20100326 Firefox/3.6");
    curl_setopt($login, CURLOPT_TIMEOUT, 400);
    curl_setopt($login, CURLOPT_RETURNTRANSFER, TRUE);
    if ($proxystatus == 'on') {
        curl_setopt($login, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($login, CURLOPT_HTTPPROXYTUNNEL, TRUE);
        curl_setopt($login, CURLOPT_PROXY, $proxy); 
    }
    curl_setopt($login, CURLOPT_URL, $url);
    curl_setopt($login, CURLOPT_HEADER, TRUE);
	curl_setopt($login,CURLOPT_REFERER,"http://google.com");
//    curl_setopt($login, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($login, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($login, CURLOPT_POST, TRUE);
	curl_setopt ($login, CURLOPT_RETURNTRANSFER, TRUE); 
    curl_setopt($login, CURLOPT_POSTFIELDS, $data);
    ob_start();      // prevent any output
    return curl_exec ($login); // execute the curl command
    ob_end_clean();  // stop preventing output
    curl_close ($login);
    unset($login);    
}  
function curl_grab_page($site,$proxy= '',$proxystatus = 'off'){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    if ($proxystatus == 'on') {
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, TRUE);
        curl_setopt($ch, CURLOPT_PROXY, $proxy); 
    }
	curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIES);
    curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIES);
    curl_setopt($ch, CURLOPT_URL, $site);
    ob_start();      // prevent any output
    return curl_exec ($ch); // execute the curl command
    ob_end_clean();  // stop preventing output
    curl_close ($ch);
}  



function getUrl($url){

	$cookieFile = dirname(__FILE__).DIRECTORY_SEPARATOR.'cookie.txt';
	$curl = curl_init();
	// Set CURL cookie option
	curl_setopt($curl, CURLOPT_COOKIEJAR, $cookieFile);
	curl_setopt($curl, CURLOPT_COOKIEFILE, $cookieFile);
	curl_setopt($curl, CURLOPT_URL, $forumUrl);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($curl, CURLOPT_HEADER, 0);
	return curl_exec($curl);
}
 $myarr= array(
            'base_url' => 'http://www.vietmanga.vn',
            'topic_url' => 'http://www.vietmanga.vn/main-forum-2/aaa3333333333333-8/',
            'topic_id' => 8,
            'username' => 'Kai',
            'password' => 'lovecomputer');


//doPost4rum($myarr,'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa','bodyyyyyyyyyyyyyy&yyyyyyyyyyy^^yyyyyyyyyyyyyyyyyyyyyy');
/*
	ham nay thuc thi tac vu dang nhap va post 1 bai den 1 topic nhat dinh
	thong so dau gom co:
		1 mang thong tin dang nhap : base url:noi chua 4rum,topic url,topic id,u,p
		title
		post body
*/
function doPost4rum($iarr = array(),$ptitle="",$pbody=""){
	//kiem tra xem dieu kien post co hop le hay khong
	if( empty($iarr) || (strlen($ptitle) < 10) || (strlen($pbody) < 10) ){
		return -1;
	}else{

		//login 4rum
		$postLogins   = array(
							"vb_login_username={$iarr['username']}",
							"vb_login_password={$iarr['password']}",
							"securitytoken=guest",
							"do=login",
							"cookieuser=1"
						);
		$loginDatas = implode('&', $postLogins);
		curl_postaction($iarr['base_url'].'/login.php?do=login',$loginDatas,$proxy,$proxystatus);
		//get security token
		$html = curl_grab_page($iarr['topic_url'],$proxy,$proxystatus); 
		$math = preg_match("/var\s?SECURITYTOKEN\s?=\s?\"(.*?)\"/ims", $html, $matches);
//		echo $matches[1];
		//post data
		$postFields   = 	array(
							 "title"=>$ptitle,
							 "message"=>$pbody,
							 "do"=>"postreply"
							 ,"securitytoken"=>"{$matches[1]}"
							 );
		$postDatas = implode('&', $postFields);
//		echo curl_postaction("http://localhost/test/checkpost.php",$postFields,$proxy,$proxystatus);
		$postUrl = $iarr['base_url']."/newreply.php?do=postreply&t=".$iarr['topic_id'];
		curl_postaction($postUrl,$postFields,$proxy,$proxystatus);
	}
	return 1;
}


/*
	send a header request only to a hosting.Return the code or -1 if cant send request
*/
function checkHeader($siteUrl){
	ob_start(); 
	$ch = curl_init(); // create cURL handle (ch)
	if (!$ch) {
		die("Couldn't initialize a cURL handle");
	}
	// set some cURL options
	$ret = curl_setopt($ch, CURLOPT_URL,            $siteUrl);
	$ret = curl_setopt($ch, CURLOPT_HEADER,         1);
	$ret = curl_setopt($ch, CURLOPT_NOBODY, 		1);
	$ret = curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
	$ret = curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
	$ret = curl_setopt($ch, CURLOPT_TIMEOUT,        30);

	// execute and get respond content since exec dunt return
	$ret = curl_exec($ch);
	$retrievedhtml = ob_get_contents();
	ob_end_clean();

//	echo $ret;
	if (empty($ret)) {
		// some kind of an error happened
//		echo(curl_error($ch));
		$math = preg_match('/^HTTP\/(.{4}?)(.*?)$/ism',$retrievedhtml,$match);
		return $match[2];
		curl_close($ch); // close cURL handler
	} else {
		$info = curl_getinfo($ch);
		curl_close($ch); // close cURL handler

		if (empty($info['http_code'])) {
				return -1;
		} else {
			// load the HTTP codes
			$http_codes = parse_ini_file("ser_code.ini");
			$myRetstr = $info['http_code'] . " " . $http_codes[$info['http_code']];
			return $myRetstr;
		}

	}
}
function readmyline($lines){
	$tmp = array();
	foreach($lines as $line){
		$tmplime = array();
		$myline = explode("|", $line);
		$tmplime['base_url'] = $myline[0];
		$tmplime['topic_url'] = $myline[1];
		$tmplime['topic_id'] = $myline[2];
		$tmplime['username'] = $myline[3];
		$tmplime['password'] = $myline[4];
		array_push($tmp,$tmplime);
	}
	return $tmp;

}

function unique_url($lines){
	$tmp = array();
	foreach($lines as $line){
			/*** get the host name from a url ***/
			preg_match('#^(http://|https://)?([^/]+)#i', $line, $matches);
			array_push($tmp,trim($matches[0]));
	}
	sort($tmp);
	$tmp = array_unique($tmp);
	$tmp = array_values($tmp);
	return $tmp;

}

?>